'use strict';

//获取数据库索引
const db = uniCloud.database()
const dbCmd = db.command  //操作符
exports.main = async (event, context) => {
	const {
		user_id,
		author_id
	} = event
	
	const user= await db.collection('user').doc(user_id).get()
	// console.log(user.data[0].author_likes_ids)
	const author_likes_ids = user.data[0].author_likes_ids
	
	// console.log(author_likes_ids.includes(author_id))
	// console.log('author_likes_ids:',author_likes_ids)
	// console.log('author_id:',author_id)
	let author_ids = null
	if(author_likes_ids.includes(author_id)){//操作作者id 是否在关注里面
		author_ids = dbCmd.pull(author_id) //pull删除数据
	}else{
		author_ids = dbCmd.addToSet(author_id) // addToSet将数据追加到数组里（已经有存在这个数据，则不会有操作）
	}
	
	await db.collection('user').doc(user_id).update({
		author_likes_ids :author_ids
	})
	
	//返回数据给客户端
	return {
		code:200,
		message:"更新成功",
	}
};
